package com.demo.business.userlogin;

import com.tiangong.framework.annotation.OpenApi;
import com.tiangong.framework.bo.CDO;
import com.tiangong.framework.bo.Return;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description: 
 * @create: 2022-02-25 23:30:08
 **/
@Service("userlogin")
@Slf4j
public class UserloginService{

    @Resource
    UserloginDao userloginDao;

    /**
     * 添加,获取lId数据
     *
     * @param cdoRequest - lId:id
     *                   - lUserId:用户Id
     *                   - sLoginName:
     *                   - sPwd:密码
     *                   - sLoginMode:mobile/QQ/wx
     *                   - sRegistDevice:注册设备：IOS、Android
     *                   - sRemark:备注
     *                   - nStatus:状态：0未授权,1、正常，99锁定
     *                   - nAuthType111:授权方式：0未授权, 1、验证码，2、会员
     *                   - dCreateTime:创建时间
     *                   - dLoginTime:更新时间
     *                   - nLock:是否锁定用户，0正常，99锁定
     * @return
     * @author 
     * @create: 2022-02-25 23:30:08
     */
    @OpenApi
    public Return addUserlogin(CDO cdoRequest) {
        userloginDao.addUserlogin(cdoRequest);
        CDO cdoResult = new CDO();
        if (cdoRequest.containsKey("lId")) {
            cdoResult.set("lId", cdoRequest.get("lId"));
        }
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据lId，更新数据
     *
     * @param cdoRequest - lId:id
     *                   - lUserId:用户Id
     *                   - sLoginName:
     *                   - sPwd:密码
     *                   - sLoginMode:mobile/QQ/wx
     *                   - sRegistDevice:注册设备：IOS、Android
     *                   - sRemark:备注
     *                   - nStatus:状态：0未授权,1、正常，99锁定
     *                   - nAuthType111:授权方式：0未授权, 1、验证码，2、会员
     *                   - dCreateTime:创建时间
     *                   - dLoginTime:更新时间
     *                   - nLock:是否锁定用户，0正常，99锁定
     * @return
     * @author 
     * @create: 2022-02-25 23:30:08
     */
    @OpenApi
    public Return updateUserloginById(CDO cdoRequest) {
        int affectedRows = userloginDao.updateUserloginById(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据lId，删除数据
     *
     * @param cdoRequest - lId:id
     * @return
     * @author 
     * @create: 2022-02-25 23:30:08
     */
    @OpenApi
    public Return deleteUserloginById(CDO cdoRequest){
        int affectedRows = userloginDao.deleteUserloginById(cdoRequest.getLong("lId"));
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据条件，删除数据
     *
     * @param cdoRequest - lUserId:用户Id
     *                   - sLoginName:
     * @return
     * @author 
     * @create: 2022-02-25 23:30:08
     */
    //@OpenApi
    public Return deleteUserlogin(CDO cdoRequest) {
        int affectedRows = userloginDao.deleteUserlogin(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据ID，获取数据
     *
     * @param cdoRequest - lId:id
     * @return
     * @author 
     * @create: 2022-02-25 23:30:08
     */
    @OpenApi
    public Return getUserloginById(CDO cdoRequest) {
        CDO cdoMapperResult = userloginDao.getUserloginById(cdoRequest.getLong("lId"));
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据混合条件，获取数据
     *
     * @param cdoRequest - lId:id
     *                   - lUserId:用户Id
     *                   - sLoginName:
     * @return
     * @author 
     * @create: 2022-02-25 23:30:08
     */
    @OpenApi
    public Return getUserlogin(CDO cdoRequest) {
        CDO cdoMapperResult = userloginDao.getUserlogin(cdoRequest);
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据非空条件，获取数据列表
     *
     * @param cdoRequest - lUserId:用户Id
     *                   - sLoginName:
     * @return
     * @author 
     * @create: 2022-02-25 23:30:08
     */
    @OpenApi
    public Return getUserloginList(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = userloginDao.getUserloginList(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
        int total = userloginDao.getUserloginListCount(cdoRequest);
        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据混合条件，获取数据列表
     *
     * @param cdoRequest - lId:id
     *                   - lUserId:用户Id
     *                   - sLoginName:
     * @return
     * @author 
     * @create: 2022-02-25 23:30:08
     */
    @OpenApi
    public Return getUserloginListByCondition(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = userloginDao.getUserloginListByCondition(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
//        int total = userloginDao.getUserloginListByConditionCount(cdoRequest);
//        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }
}
